package com.yuneec.fly.fragments;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.yuneec.fly.GlobalParams;
import com.yuneec.fly.R;
import com.yuneec.fly.events.EventBusFactory;
import com.yuneec.fly.events.uiEvent.SetLeftPhotoCountEvent;
import com.yuneec.fly.events.uiEvent.SetSwitchCameraSTBEnableEvent;
import com.yuneec.fly.events.uiEvent.SwitchCameraModeEvent;
import com.yuneec.fly.events.uiEvent.UpdateRecordTimeEvent;
import com.yuneec.fly.presenters.CameraPresenter;
import com.yuneec.fly.presenters.Cgo4Presenter;
import com.yuneec.fly.views.SyncToggleButton;
import com.yuneec.fly.views.ZoomOperationView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class CameraControllerFragment extends BaseFragment {
    private static final int UPDATE_CURRENT_ZOOM = 1;
    private SyncToggleButton switchCameraSTB;
    private ZoomOperationView zoomOV;
    private CameraPresenter cameraPresenter;

    private TextView leftPhotoCountTV;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UPDATE_CURRENT_ZOOM:
                    zoomOV.setCurrentValue(msg.obj + "mm");
                    break;
            }
        }
    };

    public void setCameraPresenter(CameraPresenter cameraPresenter) {
        this.cameraPresenter = cameraPresenter;
        if (cameraPresenter instanceof Cgo4Presenter) {
//            ((Cgo4Presenter) cameraPresenter).setOnUdpUpdateListener(new Cgo4Presenter.OnUdpUpdateListener() {
//
//                @Override
//                public void onUpdateZoom(int zoom) {
//                    handler.obtainMessage(UPDATE_CURRENT_ZOOM, zoom).sendToTarget();
//                }
//            });
            zoomOV.setVisibility(View.VISIBLE);
            leftPhotoCountTV.setVisibility(View.VISIBLE);
        } else {
            zoomOV.setVisibility(View.GONE);
            leftPhotoCountTV.setVisibility(View.GONE);
        }
    }

    @Override
    protected int getContentViewResourceId() {
        return R.layout.fragment_camera_controller;
    }

    @Override
    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        leftPhotoCountTV = (TextView) selfView.findViewById(R.id.leftPhotoCountTV);
        switchCameraSTB = (SyncToggleButton) selfView.findViewById(R.id.switchCameraSTB);
        switchCameraSTB.setOnUpdateChangeListener(new SyncToggleButton.OnUpdateChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked, boolean byUser) {
                if (cameraPresenter != null && byUser) {
                    cameraPresenter.setCameraMode(isChecked ? CameraPresenter.CameraMode.VIDEO : CameraPresenter.CameraMode.PHOTO);
                }
            }
        });
        zoomOV = (ZoomOperationView) selfView.findViewById(R.id.zoomOV);
        zoomOV.setOnZoomChangedListener(new ZoomOperationView.onZoomOperatedListener() {

            @Override
            public void onOperated(int operate) {
                if (cameraPresenter instanceof Cgo4Presenter) {
                    switch (operate) {
                        case ZoomOperationView.ZOOM_OPERATION_WIDE:
//                            cameraPresenter.zoom(ZoomCommand.Mode.WIDE);
                            break;
                        case ZoomOperationView.ZOOM_OPERATION_TELE:
//                            cameraPresenter.zoom(ZoomCommand.Mode.TELE);
                            break;
                        case ZoomOperationView.ZOOM_OPERATION_STOP:
//                            cameraPresenter.stopZoom();
                            break;
                    }
                }
            }
        });
        selfView.setVisibility(View.GONE);
    }

    @Override
    public void onStart() {
        EventBusFactory.getUiBus().register(this);
        super.onStart();
    }

    @Override
    public void onStop() {
        EventBusFactory.getUiBus().unregister(this);
        super.onStop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUpdateRecordTimeView(UpdateRecordTimeEvent event) {
        if (event.isRecording) {
            switchCameraSTB.setEnabled(false);
        } else {
            switchCameraSTB.setEnabled(true);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSetLeftPhotoCount(final SetLeftPhotoCountEvent event) {
        if (GlobalParams.cameraType == GlobalParams.CameraType.CGO4) {
            leftPhotoCountTV.setText("[ " + (event.leftPhotoCount > 0 ? event.leftPhotoCount : "N/A") + " ]");
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSwitchCameraMode(final SwitchCameraModeEvent event) {
        setSwitchCameraSTBStatus(event.cameraMode);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSetSwitchCameraSTBEnable(final SetSwitchCameraSTBEnableEvent event) {
        switchCameraSTB.setEnabled(event.enable);
    }

    public void setSwitchCameraSTBStatus(CameraPresenter.CameraMode status) {
        switchCameraSTB.syncState(status == CameraPresenter.CameraMode.VIDEO);
        if (status == CameraPresenter.CameraMode.VIDEO) {
            if (GlobalParams.cameraType == GlobalParams.CameraType.CGO4) {
                leftPhotoCountTV.setVisibility(View.GONE);
            }
        } else {
            if (GlobalParams.cameraType == GlobalParams.CameraType.CGO4) {
                leftPhotoCountTV.setVisibility(View.VISIBLE);
            }
        }
    }

    public void changeVisable(boolean visible) {
        if (cameraPresenter != null && visible) {
            selfView.setVisibility(View.VISIBLE);
            selfView.bringToFront();
        } else {
            selfView.setVisibility(View.GONE);
        }
    }
}
